<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;
use app\common\model\School as SchoolModel;
use app\common\service\GuguService;
use OSS\OssClient;
use OSS\Core\OssException;
use app\common\service\ZhiyuanService;

class Temp extends App
{
    public function ceshi(){
        $enrollprovince = '湖北';
        $province_id = 1709;
        $user_id=5;
		$batch=1;
		$year = date('Y',strtotime("-1 year"));
		$member = Db::name("member")->field('province_id,cj_score,cj_subjects,cj_subjects1,cj_subject_mode,cj_subject_mode_title,cj_rank,cj_subject_mode_title_2024,cj_subject_mode_title_2023')->where('id',$user_id)->find();
		$score_min = $member['cj_score']-30;
		$score_max = $member['cj_score']+20;
		if(empty($member['cj_rank'])){
			return [0=>'500',1=>'请先创建成绩'];
		}
		$type = $member['cj_subject_mode_title_2024'];
		
		//去年院校数据
		$list = Db::name("school_line_new")->where('province_id',$province_id)->where('year',$year)->where('type_name',$type)->where('lowest_rank','>',0)->whereBetween('lowest_score', [$score_min,$score_max])->field('id,school_uuid,college_name,cover_image,school_type as school_xingzhi,province,school_in_city as city,course_selection_name as subject_group,lowest_score as score_min,lowest_rank as weici_min,admission_batch')->order('id asc')->select();  
		$school_uuids = array_column($list,'school_uuid');
		dump('总数'.count($list));
		$school_list = Db::name("school")->field('school_uuid,id,school_suoshu,college_code,school_type,province')->where('school_uuid','in',$school_uuids)->select();
		$school_ids = array_column($school_list,'id');
		$school_suoshu_ed = array_column($school_list,'school_suoshu','school_uuid');
		$school_id_ed = array_column($school_list,'id','school_uuid');
		$school_type_ed = array_column($school_list,'school_type','school_uuid');
		$school_code_ed = array_column($school_list,'college_code','school_uuid');
		$province_ed = array_column($school_list,'province','school_uuid');
		$school_tese = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id','in',$school_ids)->field('d.school_id,t.title')->select();
		$school_tese_new = [];
		foreach($school_tese as $kt=>$vt){
			$school_tese_new[$vt['school_id']][] = $vt['title'];
			
		}
		$school_type_arr = Db::name('school_type')->column('id,title');
		$suoshu_list = SchoolModel::getSuoshuList();	
		
		$year1 = date('Y',strtotime("-2 year"));
		$year2 = date('Y',strtotime("-3 year"));
		
		//获取去年专业数据
		$score_min1 = $score_min-80;
		$score_max1 = $score_max+80;
		//$major_list = Db::name("major_line_new")->where('province_id',$province_id)->where('year',$year)->where('type_name',$type)->where('school_uuid','in',$school_uuids)->where('lowest_section','>',0)->whereBetween('lowest_score', [$score_min1,$score_max1])->field('school_uuid,lowest_score as score_min,lowest_section as weici_min,major_name,major_code,batch_name,subject_group')->order('id asc')->select();
		$major_list = Db::name("major_line_new")->where('province_id',$province_id)->where('year',$year)->where('type_name',$type)->where('school_uuid','in',$school_uuids)->whereBetween('lowest_score', [$score_min1,$score_max1])->field('school_uuid,lowest_score as score_min,lowest_section as weici_min,major_name,major_code,batch_name,subject_group')->order('id asc')->select();
	//	dump(($major_list));die;
		$major_list_new = [];
		foreach($major_list as $km=>$vm){
			if(empty($member['cj_rank'])){
				$dengxiao_weicha = '-';
			}else{
				if(!empty($vm['weici_min']) && is_numeric($vm['weici_min'])){
					$dengxiao_weicha = $vm['weici_min']-$member['cj_rank'];
				}else{
					$dengxiao_weicha = '-';
				}
			}
			
			$vm['dengxiao_weicha'] = $dengxiao_weicha;
			$vm['linecha'] = '-';
			$vm['plan_nums'] = '-';
			$vm['subject_group'] = filter_subject_group($vm['subject_group']);
			$major_list_new[$vm['school_uuid'].$vm['subject_group']][] = $vm;
		}
		
		//获取23年专业数据
		//$major_list_23 = Db::name("major_line_new")->where('province_id',$province_id)->where('year',$year1)->where('type_name',$type)->where('school_uuid','in',$school_uuids)->where('lowest_section','>',0)->whereBetween('lowest_score', [$score_min1,$score_max1])->field('school_uuid,lowest_score as score_min,lowest_section as weici_min,major_name,major_code,batch_name,subject_group')->order('id asc')->select();
		$major_list_23 = Db::name("major_line_new")->where('province_id',$province_id)->where('year',$year1)->where('type_name',$type)->where('school_uuid','in',$school_uuids)->whereBetween('lowest_score', [$score_min1,$score_max1])->field('school_uuid,lowest_score as score_min,lowest_section as weici_min,major_name,major_code,batch_name,subject_group')->order('id asc')->select();
	//	dump(($major_list));die;
		$major_list_new_23 = [];
		foreach($major_list_23 as $km=>$vm){
			if(empty($member['cj_rank'])){
				$dengxiao_weicha = '-';
			}else{
				if(!empty($vm['weici_min']) && is_numeric($vm['weici_min'])){
					$dengxiao_weicha = $vm['weici_min']-$member['cj_rank'];
				}else{
					$dengxiao_weicha = '-';
				}
			}
			
			$vm['dengxiao_weicha'] = $dengxiao_weicha;
			$vm['linecha'] = '-';
			$vm['plan_nums'] = '-';
			$vm['subject_group'] = filter_subject_group($vm['subject_group']);
			$major_list_new_23[$vm['school_uuid'].$vm['subject_group'].$vm['batch_name']][] = $vm;
		}
		//获取22年专业数据
		//$major_list_22 = Db::name("major_line_new")->where('province_id',$province_id)->where('year',$year2)->where('type_name',$type)->where('school_uuid','in',$school_uuids)->where('lowest_section','>',0)->whereBetween('lowest_score', [$score_min1,$score_max1])->field('school_uuid,lowest_score as score_min,lowest_section as weici_min,major_name,major_code,batch_name,subject_group')->order('id asc')->select();
		$major_list_22 = Db::name("major_line_new")->where('province_id',$province_id)->where('year',$year2)->where('type_name',$type)->where('school_uuid','in',$school_uuids)->whereBetween('lowest_score', [$score_min1,$score_max1])->field('school_uuid,lowest_score as score_min,lowest_section as weici_min,major_name,major_code,batch_name,subject_group')->order('id asc')->select();
	//	dump(($major_list));die;
		$major_list_new_22 = [];
		foreach($major_list_22 as $km=>$vm){
			if(empty($member['cj_rank'])){
				$dengxiao_weicha = '-';
			}else{
				if(!empty($vm['weici_min']) && is_numeric($vm['weici_min'])){
					$dengxiao_weicha = $vm['weici_min']-$member['cj_rank'];
				}else{
					$dengxiao_weicha = '-';
				}
			}
			
			$vm['dengxiao_weicha'] = $dengxiao_weicha;
			$vm['linecha'] = '-';
			$vm['plan_nums'] = '-';
			$vm['subject_group'] = filter_subject_group($vm['subject_group']);
			$major_list_new_22[$vm['school_uuid'].$vm['subject_group'].$vm['batch_name']][] = $vm;
		}
		//dump(($major_list_new));die;
		$listsssss=[];
		if(!empty($list)){
			foreach($list as $k=>$v){
				$school_suoshu = !empty($school_suoshu_ed[$v['school_uuid']])?$school_suoshu_ed[$v['school_uuid']]:'';
				$school_id = !empty($school_id_ed[$v['school_uuid']])?$school_id_ed[$v['school_uuid']]:'';
				$school_tese = !empty($school_tese_new[$school_id])?$school_tese_new[$school_id]:'';
				$school_type = !empty($school_type_ed[$v['school_uuid']])?$school_type_ed[$v['school_uuid']]:'';
				$school_type_title = !empty($school_type_arr[$school_type])?$school_type_arr[$school_type]:'';
				$school_code = !empty($school_code_ed[$v['school_uuid']])?$school_code_ed[$v['school_uuid']]:'';
				$province = !empty($province_ed[$v['school_uuid']])?$province_ed[$v['school_uuid']]:'';
		
				if(empty($member['cj_rank'])){
					$dengxiao_weicha = '-';
				}else{
					if(!empty($v['weici_min']) && is_numeric($v['weici_min'])){
						$dengxiao_weicha = $v['weici_min']-$member['cj_rank'];
					}else{
						$dengxiao_weicha = '-';
					}
				}
				$list[$k]['school_code'] = $school_code; 
				$list[$k]['school_type'] = $school_type_title;
				$list[$k]['school_suoshu'] = !empty($suoshu_list[$school_suoshu])?$suoshu_list[$school_suoshu]:'';
				$list[$k]['school_tese'] = $school_tese;
				$list[$k]['dengxiao_weicha'] = $dengxiao_weicha;
				$list[$k]['linecha'] = '-';
				$list[$k]['province'] = $province; 
				
				//匹配去年的专业数据
				$v['subject_group'] = filter_subject_group($v['subject_group']);
				if(empty($major_list_new[$v['school_uuid'].$v['subject_group']]) && empty($major_list_new_23[$v['school_uuid'].$v['subject_group'].$v['admission_batch']]) && empty($major_list_new_22[$v['school_uuid'].$v['subject_group'].$v['admission_batch']])){
					
					//dump($list[$k]);die;
					//if($v['college_name']=='中国传媒大学'){
					//	echo $v['school_uuid'].$v['subject_group'].$v['admission_batch'];
					//	dump($v);die;
					//}
					$listsssss[] = $v['college_name'].$v['subject_group'].$v['admission_batch'];
					unset($list[$k]);
				}else{
					if(!empty($major_list_new[$v['school_uuid'].$v['subject_group'].$v['admission_batch']])){
						$list[$k]['major_list'] = array_unique_two($major_list_new[$v['school_uuid'].$v['subject_group'].$v['admission_batch']]);//咕咕数据有重复的需要去重
					}else{
						$list[$k]['major_list'] = [];
					}
					
				}
			}
				
		}	//dump(array_unique(array_column($list,'admission_batch')));die;
		//dump($listsssss);die;
		
		dump('专业数'.count($major_list_new));
		//dump(count($major_list_new_23));
		//dump(count($major_list_new_22));
		dump('匹配专业后'.count($list));
		if($batch == 1){
			$batchname = SchoolModel::getBenkeBtachs();
			$batch_score_name = '本科,本二,1段';
		}elseif($batch == 2){
			$batchname = SchoolModel::getZhuankeBtachs();
			$batch_score_name = '专科,2段';
		}else{
			return $this->show(2001,"参数batch错误!", '');
		}
		$batchnames = explode(',',$batchname);
		foreach($list as $k=>$v){
			if(!in_array($v['admission_batch'],$batchnames)){
				unset($list[$k]);
				continue;
			}
		}
		
		dump('匹配批次后'.count($list));die;
		return [0=>'200',1=>$list];
	
    }
	//只考虑专业分数线
    public function ceshi2(){
        $enrollprovince = '湖北';
        $province_id = 1709;
        $user_id=5;
		$year = date('Y',strtotime("-1 year"));
		$member = Db::name("member")->field('province_id,cj_score,cj_subjects,cj_subjects1,cj_subject_mode,cj_subject_mode_title,cj_rank,cj_subject_mode_title_2024,cj_subject_mode_title_2023')->where('id',$user_id)->find();
		$score_min = $member['cj_score']-50;
		$score_max = $member['cj_score']+50;
		if(empty($member['cj_rank'])){
			return [0=>'500',1=>'请先创建成绩'];
		}
		$type = $member['cj_subject_mode_title_2024'];
		
		
		$score_min1 = $score_min-30;
		$score_max1 = $score_max+30;
		$major_list = Db::name("major_line_new")->where('province_id',$province_id)->where('year',$year)->where('type_name',$type)->whereBetween('lowest_score', [$score_min1,$score_max1])->field('school_uuid,lowest_score as score_min,lowest_section as weici_min,major_name,major_code,batch_name,subject_group')->group('school_uuid')->order('id asc')->select();
		dump(count($major_list));die;
		
	
    }
	//图片上传
    public function uploadimg()
    {
        $file = request()->file('file');
        if(empty($file)){
            return $this->show(2001, '请选择图片!','');
        }
        
		//dump($file);die;
        $info = $file->validate(['size'=>1024*1024*1000,'ext'=>'jpg,png,jpeg'])->move( '../public/uploads');
        if($info){
			$getSaveName = str_replace("\\","/",$info->getSaveName());//把反斜杠(\)替换成斜杠(/)
            return $this->show(1,"success", ['src'=>'/uploads/'.$getSaveName]);
        }else{
            return $this->show(2001, $file->getError(),'');
        }
        
    } 
	
    public function uploadimg_oss() {
		//$file = request()->file('file');
		$file = request()->file('file');
        if (!$file) return $this->show(2001, '请选择图片!','');
        
        $config = config('aliyun_oss');
        try {
            $ossClient = new OssClient(
                $config['KeyId'],
                $config['KeySecret'],
                $config['Endpoint']
            );
            
            // 生成唯一文件名：目录/年月日/随机ID.后缀
            $extension = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
            $object =  date('Ymd') . '/' . uniqid() . '.' . $extension;
            // 执行上传（直接使用临时文件路径）
            $result = $ossClient->uploadFile(
                $config['Bucket'],
                $object,
                $file->getRealPath()
            );
            
			if(empty($result['info']['url'])){
				throw new \think\Exception('oss接口出错');
			}
            
            
        } catch (OssException $e) {
			return $this->show(2001, $e->getMessage(),'');
        }
		
		if(strpos($result['info']['url'],'http://') !== false)$result['info']['url'] = str_replace("http://", "https://", $result['info']['url']);
		// 返回OSS文件URL
		/* return [
			'code' => 0,
			'data'  => ['src'=>$result['info']['url']] // 公开访问URL
		]; */
		
		return $this->show(1,"success", ['src'=>$result['info']['url']]);
    }

	
    public function save_img_fengcai()
    {
	
        if($this->request->isPost()){
			$school_id = $this->request->post("school_id",0,'intval');
			$img_fengcai = $this->request->post("img_fengcai",'','trim');
			if(empty($school_id)){
				return $this->show(2001,"缺少school_id!", '');
			}
			$res = Db::name("school")->where('id',$school_id)->update(['img_fengcai'=>$img_fengcai]);
			if( $res !== false ){
				return $this->show(1,"success", '');
			}else{
				return $this->show(2002, "fail", "");
			} 
		}else{
            return $this->show(2001, "非法请求", "");
        }
	}	
	
	//更改一对一订单状态
	public function save_onetoone_status()
    {
        $user_id = $this->user_id;
        if($this->request->isPost()){
			$data = $this->request->post();
			if(empty($data['order_id'])){
				return $this->show(2001,"缺少order_id", '');
			}
			$status = $data['status']??1;
			$course_order = Db::name("course_order")->where('id',$data['order_id'])->where('type',2)->where('course_id',2)->field('id,status')->find();
			//dump(Db::name("course_order")->getlastsql());die;
			if( empty($course_order)){
				return $this->show(2001,"未找到订单	", '');
			}
	
			$res = Db::name("course_order")->where('id',$data['order_id'])->update(['status'=>$status]);
			
			if($res !== false){
				return $this->show(1,"success");
			}else{
				return $this->show(2002, "fail", "");
			}
		}else{
            return $this->show(2001,"非法请求", '');
        }	
    }
	
	//获取院校缓存数据
	public function get_zhiyuan_school_list()
    {
		$user_id = $this->request->get("user_id",0,'intval');
		if(empty($user_id)){
			return $this->show(2001,"缺少user_id", '');
		}
		if(cache('school_'.$user_id)){
			$list = cache('school_'.$user_id);
		}else{
			return $this->show(2001,"没有找到数据，请先保存", '');
		}
		//dump($list);die;
		//$list = array_column($data,'college_name');
		$list_new = []; 
		foreach ($list as $k=>$v){
		    $list_new[] = $v['college_name'].$v['subject_group'];
		}
		//dump($list);die;
		rsort($list_new);
		
		$count = count($list_new);
		return $this->show(1,"success",['count'=>$count,'list'=>$list_new]);
	}
}	